package br.edu.ufc.deti.broker.autenticacao;

import java.io.IOException;
import java.sql.*;

import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

import br.edu.ufc.deti.beans.Autenticacao;
import br.edu.ufc.deti.beans.PerfClusters;
import br.edu.ufc.deti.broker.mapeamento.ExecucaoMapping;
import br.edu.ufc.deti.broker.mapeamento.bean.PerfClustersDecorator;
import br.edu.ufc.deti.broker.persistencia.PrsAutenticacao;
import br.edu.ufc.deti.broker.persistencia.PrsAutorizacao;
import br.edu.ufc.deti.connection.DBConnection;
import br.edu.ufc.deti.util.Utils;

public class NgcAutenticacao {
	
	private PerfClusters perfClusters;
	private PerfClustersDecorator perfClustersDecorator = new PerfClustersDecorator(Utils.populaPerfClusters());
	
	public String autenticaUsuario( String xml ) {
		String retorno = "";
		PrsAutenticacao pa = new PrsAutenticacao();
		try {
			perfClusters = ExecucaoMapping.getInstance().unmarshalPerfClusters(xml, perfClustersDecorator);
			DBConnection dbc = new DBConnection("/home/rafael/workspace/perfclusters/database/perfcluster");
			Statement stmt = dbc.getStm();
			Autenticacao autenticacao = perfClusters.getAutenticacao();
			pa.autenticaUsuario(stmt, perfClusters.getAutenticacao());
			if ( autenticacao.getID() > 0 ){
				perfClusters.setRecursos(PrsAutorizacao.getInstance().getRecursosUsuario(stmt, autenticacao));
			}
			perfClustersDecorator.setPerfClusters(perfClusters);
			retorno = ExecucaoMapping.getInstance().marshalPerfClusters(perfClustersDecorator);
		} catch (MarshalException e) {
			e.printStackTrace();
		} catch (ValidationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MappingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return retorno;
	}
	public static void main(String[] args) {
		NgcAutenticacao autenticacao = new NgcAutenticacao();
		System.out.println(autenticacao.autenticaUsuario(""));
	}
}
